home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP11.ZIP / CHAP11 / POLYLINE / IPERSTOR.CPP < prev    next >
C/C++ Source or Header  |  1993-06-17  |  7KB  |  274 lines

  1. /*
  2.  * IPERSTOR.CPP
  3.  * Polyline Component Object Chapter 11
  4.  *
  5.  * Implementation of the IPersistStorage interface that we expose on the
  6.  * Polyline object.
  7.  *
  8.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Software Design Engineer
  11.  * Microsoft Systems Developer Relations
  12.  *
  13.  * Internet  :  kraigb@microsoft.com
  14.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  15.  */
  16.  
  17.  
  18. #include "polyline.h"
  19.  
  20.  
  21. /*
  22.  * CImpIPersistStorage:CImpIPersistStorage
  23.  * CImpIPersistStorage::~CImpIPersistStorage
  24.  *
  25.  * Constructor Parameters:
  26.  *  pObj            LPCPolyline pointing to the object we live in.
  27.  *  punkOuter       LPUNKNOWN of the controlling unknown.
  28.  */
  29.  
  30. CImpIPersistStorage::CImpIPersistStorage(LPCPolyline pObj, LPUNKNOWN punkOuter)
  31.     {
  32.     m_cRef=0;
  33.     m_pObj=pObj;
  34.     m_punkOuter=punkOuter;
  35.     return;
  36.     }
  37.  
  38.  
  39. CImpIPersistStorage::~CImpIPersistStorage(void)
  40.     {
  41.     return;
  42.     }
  43.  
  44.  
  45.  
  46.  
  47. /*
  48.  * CImpIPersistStorage::QueryInterface
  49.  * CImpIPersistStorage::AddRef
  50.  * CImpIPersistStorage::Release
  51.  *
  52.  * Purpose:
  53.  *  Standard set of IUnknown members for this interface
  54.  */
  55.  
  56. STDMETHODIMP CImpIPersistStorage::QueryInterface(REFIID riid, LPVOID FAR *ppv)
  57.     {
  58.     return m_punkOuter->QueryInterface(riid, ppv);
  59.     }
  60.  
  61. STDMETHODIMP_(ULONG) CImpIPersistStorage::AddRef(void)
  62.     {
  63.     ++m_cRef;
  64.     return m_punkOuter->AddRef();
  65.     }
  66.  
  67. STDMETHODIMP_(ULONG) CImpIPersistStorage::Release(void)
  68.     {
  69.     --m_cRef;
  70.     return m_punkOuter->Release();
  71.     }
  72.  
  73.  
  74.  
  75.  
  76.  
  77. /*
  78.  * CImpIPersistStorage::GetClassID
  79.  *
  80.  * Purpose:
  81.  *  Returns the CLSID of the object represented by this interface.
  82.  *
  83.  * Parameters:
  84.  *  pClsID          LPCLSID in which to store our CLSID.
  85.  *
  86.  * Return Value:
  87.  *  HRESULT         NOERROR on success, error code otherwise.
  88.  */
  89.  
  90. STDMETHODIMP CImpIPersistStorage::GetClassID(LPCLSID pClsID)
  91.     {
  92.     *pClsID=m_pObj->m_clsID;
  93.     return NOERROR;
  94.     }
  95.  
  96.  
  97.  
  98.  
  99.  
  100. /*
  101.  * CImpIPersistStorage::IsDirty
  102.  *
  103.  * Purpose:
  104.  *  Tells the caller if we have made changes to this object since
  105.  *  it was loaded or initialized new.
  106.  *
  107.  * Parameters:
  108.  *  None
  109.  *
  110.  * Return Value:
  111.  *  HRESULT         Contains S_OK if we ARE dirty, S_FALSE if NOT dirty,
  112.  *                  that is, "Yes I AM dirty, or NO, I'm clean."
  113.  */
  114.  
  115. STDMETHODIMP CImpIPersistStorage::IsDirty(void)
  116.     {
  117.     return ResultFromScode(m_pObj->m_fDirty ? S_OK : S_FALSE);
  118.     }
  119.  
  120.  
  121.  
  122.  
  123.  
  124. /*
  125.  * CImpIPersistStorage::Load
  126.  *
  127.  * Purpose:
  128.  *  Instructs the object to load itself from a previously saved IStorage
  129.  *  that was handled by ::Save in another object lifetime.  This function
  130.  *  will only be called once in the object's lifetime in lieu of ::InitNew.
  131.  *  The object may hold on to pIStorage here for incremental access.
  132.  *
  133.  * Parameters:
  134.  *  pIStorage       LPSTORAGE from which to load.
  135.  *
  136.  * Return Value:
  137.  *  HRESULT         NOERROR on success, error code otherwise.
  138.  */
  139.  
  140. STDMETHODIMP CImpIPersistStorage::Load(LPSTORAGE pIStorage)
  141.     {
  142.     POLYLINEDATA    pl;
  143.     ULONG           cb;
  144.     LPSTREAM        pIStream;
  145.     HRESULT         hr;
  146.  
  147.     if (NULL==pIStorage)
  148.         return ResultFromScode(STG_E_INVALIDPOINTER);
  149.  
  150.     //We don't check ClassStg to remain compatible with other chatpers.
  151.  
  152.     //Open the CONTENTS stream
  153.     hr=pIStorage->OpenStream("CONTENTS", 0, STGM_DIRECT | STGM_READ
  154.         | STGM_SHARE_EXCLUSIVE, 0, &pIStream);
  155.  
  156.     if (FAILED(hr))
  157.         return ResultFromScode(STG_E_READFAULT);
  158.  
  159.     //Read all the data into the POLYLINEDATA structure.
  160.     hr=pIStream->Read((LPVOID)&pl, CBPOLYLINEDATA, &cb);
  161.     pIStream->Release();
  162.  
  163.     if (CBPOLYLINEDATA!=cb)
  164.         return ResultFromScode(STG_E_READFAULT);
  165.  
  166.     //DataSet now internal on CPolyline
  167.     m_pObj->DataSet(&pl, TRUE, TRUE);
  168.  
  169.     //CHAPTER11MOD
  170.     //We also need to tell the cache to load cached graphics
  171.     m_pObj->m_pDefIPersistStorage->Load(pIStorage);
  172.     //End CHAPTER11MOD
  173.     return NOERROR;
  174.     }
  175.  
  176.  
  177.  
  178.  
  179.  
  180. /*
  181.  * CImpIPersistStorage::Save
  182.  *
  183.  * Purpose:
  184.  *  Saves the data for this object to an IStorage which may
  185.  *  or may not be the same as the one previously passed to
  186.  *  ::Load, indicated with fSameAsLoad.
  187.  *
  188.  * Parameters:
  189.  *  pIStorage       LPSTORAGE in which to save our data.
  190.  *  fSameAsLoad     BOOL indicating if this is the same pIStorage
  191.  *                  that was passed to ::Load.  If it was, then
  192.  *                  objects that built up a structure in that storage
  193.  *                  do not have to regenerate the entire thing.
  194.  *
  195.  * Return Value:
  196.  *  HRESULT         NOERROR on success, error code otherwise.
  197.  */
  198.  
  199. STDMETHODIMP CImpIPersistStorage::Save(LPSTORAGE pIStorage, BOOL fSameAsLoad)
  200.     {
  201.     POLYLINEDATA    pl;
  202.     ULONG           cb;
  203.     LPSTREAM        pIStream;
  204.     HRESULT         hr;
  205.  
  206.     if (NULL==pIStorage)
  207.         return ResultFromScode(STG_E_INVALIDPOINTER);
  208.  
  209.     /*
  210.      * fSameAsLoad is not important to us since we always rewrite
  211.      * an entire stream as well as the identification tags for this
  212.      * object.  Note that we don't bother to check the ClassStg
  213.      * above in ::Load to remain compatible with other revisions
  214.      * of Polyline in other chapters.
  215.      */
  216.  
  217.     WriteClassStg(pIStorage, m_pObj->m_clsID);
  218.     WriteFmtUserTypeStg(pIStorage, m_pObj->m_cf, (*m_pObj->m_pST)[IDS_USERTYPE]);
  219.  
  220.     hr=pIStorage->CreateStream("CONTENTS", STGM_DIRECT | STGM_CREATE
  221.         | STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &pIStream);
  222.  
  223.     if (FAILED(hr))
  224.         return ResultFromScode(STG_E_WRITEFAULT);
  225.  
  226.     //DataGet now internal on CPolyline
  227.     m_pObj->DataGet(&pl);
  228.  
  229.     hr=pIStream->Write((LPVOID)&pl, CBPOLYLINEDATA, &cb);
  230.     pIStream->Release();
  231.  
  232.     //CHAPTER11MOD
  233.     //We also need to tell the cache to save cached graphics
  234.     m_pObj->m_pDefIPersistStorage->Save(pIStorage, fSameAsLoad);
  235.     //End CHAPTER11MOD
  236.  
  237.     return (SUCCEEDED(hr) && CBPOLYLINEDATA==cb) ?
  238.         NOERROR : ResultFromScode(STG_E_WRITEFAULT);
  239.     }
  240.  
  241.  
  242.  
  243.  
  244.  
  245. //CHAPTER11MOD
  246.  
  247. /*
  248.  * CImpIPersistStorage::InitNew
  249.  * CImpIPersistStorage::SaveCompleted
  250.  * CImpIPersistStorage::HandsOffStorage
  251.  *
  252.  * Passes through to the cache.
  253.  */
  254.  
  255. STDMETHODIMP CImpIPersistStorage::InitNew(LPSTORAGE pIStorage)
  256.     {
  257.     m_pObj->m_pDefIPersistStorage->InitNew(pIStorage);
  258.     return NOERROR;
  259.     }
  260.  
  261. STDMETHODIMP CImpIPersistStorage::SaveCompleted(LPSTORAGE pIStorage)
  262.     {
  263.     m_pObj->m_pDefIPersistStorage->SaveCompleted(pIStorage);
  264.     return NOERROR;
  265.     }
  266.  
  267. STDMETHODIMP CImpIPersistStorage::HandsOffStorage(void)
  268.     {
  269.     m_pObj->m_pDefIPersistStorage->HandsOffStorage();
  270.     return NOERROR;
  271.     }
  272.  
  273. //End CHAPTER11MOD
  274.